﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_PERMISSION.Base;
using BaseDAO = ERP_RORZE_TOOL.DBAccess.BaseDAO;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_PRODUCT.Product.MO
{
    public partial class frm_MOOptionEditThis : DevExpress.XtraEditors.XtraForm
    {
        private string minDate = "1/1/1753 12:00:00 AM";

        public string ID;
        public string mONo;
        public string partID;
        public string partIDNew;
        public string mOQty;
        public string deadline;
        public string machineID;
        public string optionID;
        public string lastOption;
        public string jigID;
        public string proTime;
        public string clampTime;
        public string finished;
        public string finishdate;
        public string shift;
        public string note;
        private LogUtils log;
        public string finishMachine;

        DataTable dtCm = new DataTable();
        DataTable dtOp = new DataTable();

        public frm_MOOptionEditThis()
        {
            InitializeComponent();
            log = new LogUtils();
            //this.cbOptionID.LostFocus += cbOptionID_LostFocus;
        }

        private void frm_MOOptionEdit_Load(object sender, EventArgs e)
        {
            cbMONo.Text = mONo;
            cbPartID.Text = partID;
            cbShift.Text = shift;
            txtClampTime.Text = clampTime;
            txtJigType.Text = jigID;
            txtMOQty.Text = mOQty;
            txtNote.Text = note;
            txtProTime.Text = proTime;
            checkFinished.Checked = (!finished.Equals(string.Empty)) ? bool.Parse(finished) : false;
            checkLastOption.Checked = (!lastOption.Equals(string.Empty)) ? bool.Parse(lastOption) : false;
            dateDeadline.DateTime = (!deadline.Trim().Equals(string.Empty)) ? DateTime.Parse(deadline) : DateTime.Parse(minDate);
            dateFinishDate.DateTime = (!finishdate.Trim().Equals(string.Empty)) ? DateTime.Parse(finishdate) : DateTime.Parse(minDate);
            //dateFinishDate.DateTime = DateTime.Parse(finishdate);
            partIDNew = partID;

            if (partID.Contains("+"))
            {
                partIDNew = partID.Substring(0, partID.Length - 2);
            }
            //Bindto Machine
            dtCm = BaseDAO.ExecuteQuery("Select [242_OptionData].OptionID, [242_OptionData].PartID, [242_OptionData].MachineID from [242_OptionData] where PartID = '" + partIDNew + "'");
            dtOp = BaseDAO.ExecuteQuery("Select [242_OptionData].OptionID, [242_OptionData].PartID, [242_OptionData].MachineID from [242_OptionData] where PartID = '" + partIDNew + "'");
            DataRow row = dtCm.NewRow();
            row["MachineID"] = string.Empty;
            row["OptionID"] = string.Empty;
            row["PartID"] = string.Empty;

            cbMachineID.DataSource = dtCm;
            cbMachineID.DisplayMember = "MachineID";
            cbMachineID.ValueMember = "MachineID";
            cbMachineID.Text = machineID;


            //Bindto Optiondata
            cbOptionID.DataSource = dtOp;
            cbOptionID.DisplayMember = "OptionID";
            cbOptionID.ValueMember = "OptionID";
            cbOptionID.SelectedValue = optionID;


            //đưa giá trị vào danh sách

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            AddData();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddData()
        {
            ClsMOOptionTable obj = new ClsMOOptionTable();
            if (GetData(obj))
            {

                //clspartDAO.InsertOptionData(obj);
                if (InsertData(obj) > 0)
                {

                    //write Log
                    log.Table = LogConstant.Table.Product.MOOption;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "ID: " + ID;
                    log.WriteLog();

                    MessageBox.Show("Đã sửa xong");
                    this.Close();
                }
                else
                {
                    Utilities.MessageErrorOk("An error occurred while updating the record to database. Please try again.");
                }
            }
        }

        private bool GetData(ClsMOOptionTable obj)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(txtProTime.Text.Trim()))
            {
                MessageBox.Show("Bạn phải nhập ProTime.");
                txtProTime.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtClampTime.Text.Trim()))
            {
                MessageBox.Show("Bạn phải nhập ClampTime.");
                txtClampTime.Focus();
                return false;
            }

            if (float.Parse(txtMOQty.Text) < 0 || float.Parse(txtProTime.Text) < 0 || float.Parse(txtClampTime.Text) < 0)
            {
                MessageBox.Show("ProTime, ClampTime, AnodNote phải lớn hơn bằng 0.");
                txtMOQty.Focus();
                return false;

            }

            if (!cbOptionID.Text.Trim().Equals(string.Empty))
            {
                DataTable dtCheck =
                BaseDAO.ExecuteQuery("select * from [242_MOOption] where [MONo]='" + mONo + "' and [OptionID]='" +
                                     cbOptionID.Text + "' and OptionID <> '" + optionID + "'");
                if (dtCheck.Rows.Count > 0)
                {
                    MessageBox.Show("Đã có OptionID này trong bảng MOOption");
                    return false;
                }
            }
            else
            {
                MessageBox.Show("Hãy nhập OptionID");
                txtProTime.Focus();
                return false;
            }

            #endregion

            obj.MONo = cbMONo.Text;
            obj.PartID = cbPartID.Text;
            obj.MOQty = int.Parse(txtMOQty.Text);
            obj.Deadline = dateDeadline.DateTime;

            obj.MachineID = cbMachineID.Text;
            obj.OptionID = cbOptionID.Text;
            obj.LastOption = checkLastOption.Checked;
            obj.ProTime = float.Parse(txtProTime.Text);
            obj.ClampTime = float.Parse(txtClampTime.Text);
            obj.LastOption = checkLastOption.Checked;
            obj.Finished = checkFinished.Checked;
            obj.Finishdate = dateFinishDate.DateTime;

            obj.Shift = cbShift.Text;
            obj.JigID = txtJigType.Text;
            obj.Note = txtNote.Text;

            return true;
        }

        private int InsertData(ClsMOOptionTable obj)
        {
            string deadline = (DateTime.Compare(obj.Deadline, DateTime.Parse(minDate))) <= 0 ? "null" : "'" + obj.Deadline.ToString("MM/dd/yyyy") + "'";
            string finishdate = (DateTime.Compare(obj.Finishdate, DateTime.Parse(minDate))) <= 0 ? "null" : "'" + obj.Finishdate.ToString("MM/dd/yyyy") + "'";

            string sql = "update [242_MOOption] " +
                         "set [MONo]='" + obj.MONo + "'" +
                         ",[PartID]='" + obj.PartID + "'" +
                         ",[MOQty]=" + obj.MOQty +
                         ",[Deadline]=" + deadline +
                         ",[MachineID]='" + obj.MachineID + "'" +
                         ",[OptionID]='" + obj.OptionID + "'" +
                         ",[LastOption]='" + obj.LastOption + "'" +
                         ",[JigType]='" + obj.JigID + "'" +
                         ",[ProTime]=" + obj.ProTime +
                         ",[ClampTime]=" + obj.ClampTime +
                         ",[Finished]='" + obj.Finished + "'" +
                         ",[Finishdate]=" + finishdate +
                         ",[Shift]='" + obj.Shift + "'" +
                         ",[Note]=N'" + obj.Note + "'" +
                         " where ID=" + ID;

            return BaseDAO.ExecuteNonQuery(sql);

        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private void frm_MOOptionAddThis_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.D)
                {
                    this.btnEdit.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.End)
            {
                btnEdit.Focus();
            }
            else if (e.KeyCode == Keys.Home)
            {
                cbMachineID.Focus();
            }
        }

        private void txtMOQty_Enter(object sender, EventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

    }
}